{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "source": [
    "# Error Insights\n",
    "\n",
    "Some PowerShell errors are pretty vague so this function can take the details from the last error record and send them to ChatGPT to make sense of them.  \n",
    "\n",
    "Make sure you have your `$env:OpenAIKey` set and have the PowerShellAI module installed and imported into your current PowerShell session."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "dotnet_interactive": {
     "language": "pwsh"
    },
    "polyglot_notebook": {
     "kernelName": "pwsh"
    }
   },
   "source": [
    "### Example\n",
    "\n",
    "Web errors are often buried in an exception object. Rather than digging around just send it into the AI with `Invoke-AIErrorHelper`!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "dotnet_interactive": {
     "language": "pwsh"
    },
    "polyglot_notebook": {
     "kernelName": "pwsh"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[31;1mInvoke-RestMethod: \u001b[0m\n",
      "\u001b[31;1m\u001b[36;1mLine |\u001b[0m\n",
      "\u001b[31;1m\u001b[36;1m\u001b[36;1m   4 | \u001b[0m \u001b[36;1mInvoke-RestMethod \"https://postman-echo.com/status/429\"\u001b[0m\n",
      "\u001b[31;1m\u001b[36;1m\u001b[36;1m\u001b[0m\u001b[36;1m\u001b[0m\u001b[36;1m     | \u001b[31;1m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\u001b[0m\n",
      "\u001b[31;1m\u001b[36;1m\u001b[36;1m\u001b[0m\u001b[36;1m\u001b[0m\u001b[36;1m\u001b[31;1m\u001b[31;1m\u001b[36;1m     | \u001b[31;1m{\u001b[0m\n",
      "\u001b[31;1m\u001b[36;1m\u001b[36;1m\u001b[0m\u001b[36;1m\u001b[0m\u001b[36;1m\u001b[31;1m\u001b[31;1m\u001b[36;1m\u001b[31;1m  \"status\": 429\u001b[0m\n",
      "\u001b[31;1m\u001b[36;1m\u001b[36;1m\u001b[0m\u001b[36;1m\u001b[0m\u001b[36;1m\u001b[31;1m\u001b[31;1m\u001b[36;1m\u001b[31;1m}\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "# Throw an error\n",
    "Invoke-RestMethod \"https://postman-echo.com/status/429\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "dotnet_interactive": {
     "language": "pwsh"
    },
    "polyglot_notebook": {
     "kernelName": "pwsh"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[96mWebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand\n",
      "Response status code does not indicate success: 429 (Too Many Requests).\n",
      "\u001b[0m\n",
      "\u001b[90mThis PowerShell error occurs when a user has exceeded the rate limit of requests to a web service. This is usually due to making too many requests in a short period of time. A potential solution is to use the Start-Sleep cmdlet to pause the script for a few seconds between requests. This will allow the web service to process the requests without exceeding the rate limit. For example:\n",
      "\n",
      "Invoke-RestMethod -Uri $url\n",
      "Start-Sleep -Seconds 5\n",
      "Invoke-RestMethod -Uri $url\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "Invoke-AIErrorHelper"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [
       "c#",
       "C#"
      ],
      "languageName": "C#",
      "name": "csharp"
     },
     {
      "aliases": [
       "frontend"
      ],
      "name": "vscode"
     },
     {
      "aliases": [],
      "name": "value"
     },
     {
      "aliases": [],
      "name": ".NET"
     },
     {
      "aliases": [
       "f#",
       "F#"
      ],
      "languageName": "F#",
      "name": "fsharp"
     },
     {
      "aliases": [],
      "languageName": "HTML",
      "name": "html"
     },
     {
      "aliases": [],
      "languageName": "KQL",
      "name": "kql"
     },
     {
      "aliases": [],
      "languageName": "Mermaid",
      "name": "mermaid"
     },
     {
      "aliases": [
       "powershell"
      ],
      "languageName": "PowerShell",
      "name": "pwsh"
     },
     {
      "aliases": [],
      "languageName": "SQL",
      "name": "sql"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
